/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import java.util.Date;
import java.util.List;
import org.crystalrange.dao.AuthorityDao;
import org.crystalrange.model.Authority;
import org.crystalrange.model.User;
import org.crystalrange.service.AuthorityService;
import org.crystalrange.service.UserService;

public class AuthorityServiceImpl implements AuthorityService {

    private AuthorityDao authorityDao;
    private UserService userService;

    public AuthorityDao getAuthorityDao() {
        return this.authorityDao;
    }

    public void setAuthorityDao(AuthorityDao authorityDao) {
        this.authorityDao = authorityDao;
    }
    
    public UserService getUserService() {
        return this.userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public Authority create(String username) {
        
        User currentUser = this.userService.findByUsername(username);  
        
        Authority authority = new Authority();
        authority.setCreatedBy(currentUser);
        authority.setCreatedDate(new Date());
        
        return authority;
    }
    
    public boolean save(Authority authority, String username) {
        
        User currentUser = this.userService.findByUsername(username);  
        
        authority.setUpdatedBy(currentUser);
        authority.setUpdatedDate(new Date());
        
        return this.authorityDao.save(authority);
    }

    public boolean remove(Authority authority) {
        return this.authorityDao.remove(authority);
    }

    public Authority find(Long id) {
        return this.authorityDao.find(id);
    }

    public List<Authority> findAll() {
        return this.authorityDao.findAll();
    }

    public SearchResult<Authority> searchAndCount(Search search) {
        return this.authorityDao.searchAndCount(search);
    }
    
    public List<Authority> search(Search search) {
        return this.authorityDao.search(search);
    }
    
    public Authority searchUnique(Search search) {
        return this.authorityDao.searchUnique(search);
    }
}
